//! @Author: DengLibin 
//! @Date:  2022-04-21 09:31:29
//! @Description:  返回错误

use actix_web::{Error, HttpResponse};
use rand::{thread_rng, Rng};

use crate::error::CustomError;

/// @Author: DengLibin 
/// @Date:  2022-04-21 09:33:07
/// @Description: 随机返回一个错误或正确
/// randomly returns either () or one of the 4 CustomError variants
async fn do_something_random() -> Result<(), CustomError> {
    let mut rng = thread_rng();

    // 20% chance that () will be returned by this function
    if rng.gen_bool(2.0 / 10.0) {
        Ok(())
    } else {
        Err(rand::random::<CustomError>())
    }
}

/// @Author: DengLibin 
/// @Date:  2022-04-21 09:51:48
/// @Description: 返回错误
pub async fn do_something() -> Result<HttpResponse, Error> {
    do_something_random().await?;

    Ok(HttpResponse::Ok().body("Nothing interesting happened. Try again."))
}
